package com.qing.utils.lottery.ssq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.qing.utils.lottery.ssq.bean.SsqResponse;
import com.qing.utils.lottery.ssq.bean.SsqResponseResult;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author guoqf
 */
public class SsqCalUtil {

    private static int[] winNumbers = {8, 9, 18, 19, 20, 27, 15};


    private static int[][] boughtNumbersArray = {
            {9, 17, 18, 19, 23, 26, 2},
            {7, 10, 11, 27, 28, 30, 6},
            {9, 15, 16, 19, 21, 28, 15},
            {1, 5, 9, 15, 20, 29, 4},
            {5, 16, 29, 30, 31, 32, 10}
    };


    public static void main(String[] args) {
        /*querySsqWinningList();
        match(winNumbers, boughtNumbersArray);
*/

//        matchNewest(boughtNumbersArray);


        int calCount = 50;

        List<SsqNumbers> ssqNumbers = new ArrayList<>(selectAvgRatio(calCount));
        ssqNumbers.addAll(selectHighestRatio(calCount));
        ssqNumbers.addAll(selectLowestRatio(calCount));
        int size = ssqNumbers.size();
        Set<Integer> indexes = new HashSet<>();
        Random random = new Random();
        int index;
        for (int i = 0; i < 5; i++) {
            do {
                index = random.nextInt(size);
            } while (indexes.contains(index));

            indexes.add(index);
        }

        indexes.forEach(i->{
            SsqNumbers ssqNumbers1 = ssqNumbers.get(i);
            System.out.println("选中的号码，红色: " + Arrays.toString(ssqNumbers1.getRedNumbers()) + ", 蓝色： " + ssqNumbers1.getBlueNumber());
        });


        /*
        calAvgRatio(calCount);
        calHighestRatio(calCount);
        calLowestRatio(calCount);*/
    }

    private static List<SsqResponseResult> querySsqWinningList(int count) {
//        String ssqQueryUrl = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=30&issueStart=&issueEnd=&dayStart=&dayEnd=";
        String ssqQueryUrl = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueStart=&issueEnd=&dayStart=&dayEnd=&issueCount=" + count;
        String s = HttpUtil.get(ssqQueryUrl);
        HttpRequest httpRequest = HttpUtil.createGet(ssqQueryUrl).contentType(ContentType.JSON.getValue());
        System.out.println("============双色球");
        String body;
        try (HttpResponse response = httpRequest.execute()) {
            body = response.body();
            System.out.println(body);
            System.out.println("============双色球");
            SsqResponse ssqResponse = JSONUtil.toBean(body, SsqResponse.class);
            return ssqResponse.getResult();
        }
    }


    private static SsqNumbers parseNewestSsqNumbers() {
        SsqResponseResult resultList = queryNewestSsqWinning();
        return parseSsqNumbers(resultList);
    }

    private static SsqNumbers parseSsqNumbers(SsqResponseResult ssqResult) {
        if (ssqResult != null) {
            String date = ssqResult.getDate();
            String theDay = date.substring(0, 10);
            String format = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String red = ssqResult.getRed();
            String[] reds = red.split(",");
            int[] redNumbers = new int[reds.length];
            String blue = ssqResult.getBlue();
            if (format.equals(theDay)) {
                // 今天最新的结果已出
//                System.out.println("今天最新开奖号码已出");
            } else {
//                System.out.println("本期最新开奖号码未出，上期开奖号码是：" + red + "/" + blue + "[" + date + "]");

            }
            for (int i = 0; i < reds.length; i++) {
                redNumbers[i] = Integer.parseInt(reds[i]);
            }
            int blueNumber = Integer.parseInt(blue);
            return new SsqNumbers(redNumbers, blueNumber);

        }
        return null;
    }


    private static SsqResponseResult queryNewestSsqWinning() {
        List<SsqResponseResult> resultList = querySsqWinningList(10);
        return CollUtil.isNotEmpty(resultList) ? resultList.stream().max(Comparator.comparing(r -> Integer.valueOf(r.getCode()))).get() : null;
    }


    private static void calLowestRatio(int calCount) {
        System.out.println("计算最低概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(100);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 10;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectLowestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());

        nextCount = 20;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectLowestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        nextCount = 50;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectLowestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        selected = selectLowestRatio(numbersList, 0, numbersList.size());
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        for (int i = 0; i < calCount; i++) {
            System.out.println("****************************************************************************");
            SsqNumbers ssqNumbers = numbersList.get(i);
            System.out.println("第 " + (i + 1) + " 个");
            System.out.println("开奖号码红色号码：" + Arrays.toString(ssqNumbers.getRedNumbers()) + ", 蓝色号码：" + ssqNumbers.getBlueNumber());

            //全部
            startIn = i + 1;


            nextCount = 50;

            int step = 10;

            int highestNextCount = nextCount;
            int highestPrizeClass = 0;


            int leftTotalCount = numbersList.size() - i - 1;
            while (nextCount < leftTotalCount) {
                System.out.println("在剩下" + nextCount + " 个中挑选");
                int prizeClass = matchLowestNextCount(nextCount, numbersList, ssqNumbers, startIn);
                if (prizeClass > 0 && (highestPrizeClass == 0 || prizeClass < highestPrizeClass)) {
                    highestNextCount = nextCount;
                    highestPrizeClass = prizeClass;
                }

                nextCount += step;
            }
            highestPrizeClass = 0;
            System.out.println("挑选的最高奖是[" + highestPrizeClass + "]， 在剩下[" + highestNextCount + "]中选中");
            System.out.println("****************************************************************************\n\n\n");
        }

        System.out.println("计算最低概率结束*****************************************************************");
    }


    private static void calHighestRatio(int calCount) {
        System.out.println("计算最高概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(100);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 10;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectHighestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());

        nextCount = 20;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectHighestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        nextCount = 50;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectHighestRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        selected = selectHighestRatio(numbersList, 0, numbersList.size());
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        for (int i = 0; i < calCount; i++) {
            System.out.println("****************************************************************************");
            int leftTotalCount = numbersList.size() - i - 1;
            SsqNumbers ssqNumbers = numbersList.get(i);
            System.out.println("第 " + (i + 1) + " 个");
            System.out.println("开奖号码红色号码：" + Arrays.toString(ssqNumbers.getRedNumbers()) + ", 蓝色号码：" + ssqNumbers.getBlueNumber());

            //全部
            startIn = i + 1;


            nextCount = 50;

            int step = 10;

            int highestNextCount = nextCount;
            int highestPrizeClass = 0;


            while (nextCount < leftTotalCount) {
                System.out.println("在剩下" + nextCount + " 个中挑选");
                int prizeClass = matchHighestNextCount(nextCount, numbersList, ssqNumbers, startIn);
                if (prizeClass > 0 && (highestPrizeClass == 0 || prizeClass < highestPrizeClass)) {
                    highestNextCount = nextCount;
                    highestPrizeClass = prizeClass;
                }

                nextCount += step;
            }
            if (highestPrizeClass == 100) {
                highestPrizeClass = 0;
            }
            System.out.println("挑选的最高奖是[" + highestPrizeClass + "]， 在剩下[" + highestNextCount + "]中选中");

            System.out.println("****************************************************************************\n\n\n");
        }
        System.out.println("计算最高概率结束*****************************************************************");

    }

    private static void calAvgRatio(int calCount) {
        System.out.println("计算最高概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(100);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 10;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectAvgRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());

        nextCount = 20;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectAvgRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        nextCount = 50;
        System.out.println("剩下： " + nextCount + " 个");
        selected = selectAvgRatio(numbersList, startIn, startIn + nextCount);
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
//        doMatch(ssqNumbers, Collections.singletonList(selected));

        selected = selectAvgRatio(numbersList, 0, numbersList.size());
        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        for (int i = 0; i < calCount; i++) {
            System.out.println("****************************************************************************");
            int leftTotalCount = numbersList.size() - i - 1;
            SsqNumbers ssqNumbers = numbersList.get(i);
            System.out.println("第 " + (i + 1) + " 个");
            System.out.println("开奖号码红色号码：" + Arrays.toString(ssqNumbers.getRedNumbers()) + ", 蓝色号码：" + ssqNumbers.getBlueNumber());

            //全部
            startIn = i + 1;


            nextCount = 50;

            int step = 10;

            int highestNextCount = nextCount;
            int highestPrizeClass = 0;


            while (nextCount < leftTotalCount) {
                System.out.println("在剩下" + nextCount + " 个中挑选");
                int prizeClass = matchAvgNextCount(nextCount, numbersList, ssqNumbers, startIn);
                if (prizeClass > 0 && (highestPrizeClass == 0 || prizeClass < highestPrizeClass)) {
                    highestNextCount = nextCount;
                    highestPrizeClass = prizeClass;
                }

                nextCount += step;
            }
            if (highestPrizeClass == 100) {
                highestPrizeClass = 0;
            }
            System.out.println("挑选的最高奖是[" + highestPrizeClass + "]， 在剩下[" + highestNextCount + "]中选中");

            System.out.println("****************************************************************************\n\n\n");
        }
        System.out.println("计算最高概率结束*****************************************************************");

    }


    private static List<SsqNumbers> selectLowestRatio(int calCount) {
        System.out.println("计算最低概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(1000);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 40;

        int step = 20;
        List<SsqNumbers> result = new ArrayList<>();
        while (nextCount < total) {
            System.out.println("在剩下" + nextCount + " 个中挑选");
            SsqNumbers ssqNumbers = selectLowestNextCount(nextCount, numbersList, startIn);
            result.add(ssqNumbers);
            nextCount += step;
        }


        System.out.println("计算最低概率结束*****************************************************************");
        return result;
    }


    private static List<SsqNumbers> selectHighestRatio(int calCount) {
        System.out.println("计算最高概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(1000);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 40;

        int step = 20;
        List<SsqNumbers> result = new ArrayList<>();
        while (nextCount < total) {
            System.out.println("在剩下" + nextCount + " 个中挑选");
            SsqNumbers ssqNumbers = selectHighestNextCount(nextCount, numbersList, startIn);
            result.add(ssqNumbers);
            nextCount += step;
        }


        System.out.println("计算最高概率结束*****************************************************************");

        return result;
    }

    private static List<SsqNumbers> selectAvgRatio(int calCount) {
        System.out.println("计算平均概率*****************************************************************");
        List<SsqResponseResult> resultList = querySsqWinningList(1000);
        List<SsqNumbers> numbersList = new ArrayList<>();
        for (SsqResponseResult result : resultList) {
            SsqNumbers ssqNumbers = parseSsqNumbers(result);
            numbersList.add(ssqNumbers);
        }


        int total = numbersList.size();

        SsqNumbers selected;

        int startIn = 0;
        int nextCount = 40;

        int step = 20;
        List<SsqNumbers> result = new ArrayList<>();
        while (nextCount < total) {
            System.out.println("在剩下" + nextCount + " 个中挑选");
            SsqNumbers ssqNumbers = selectAvgNextCount(nextCount, numbersList, startIn);
            result.add(ssqNumbers);
            nextCount += step;
        }


        System.out.println("计算平均概率结束*****************************************************************");
        return result;
    }

    private static int matchHighestNextCount(int nextCount, List<SsqNumbers> numbersList, SsqNumbers ssqNumbers, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        SsqNumbers selected = selectHighestRatio(numbersList, startIn, startIn + nextCount);
//        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        return doMatch(ssqNumbers, Collections.singletonList(selected));
    }


    private static int matchLowestNextCount(int nextCount, List<SsqNumbers> numbersList, SsqNumbers ssqNumbers, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        SsqNumbers selected = selectLowestRatio(numbersList, startIn, startIn + nextCount);
//        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        return doMatch(ssqNumbers, Collections.singletonList(selected));
    }

    private static int matchAvgNextCount(int nextCount, List<SsqNumbers> numbersList, SsqNumbers ssqNumbers, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        SsqNumbers selected = selectAvgRatio(numbersList, startIn, startIn + nextCount);
//        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
        return doMatch(ssqNumbers, Collections.singletonList(selected));
    }


    private static SsqNumbers selectHighestNextCount(int nextCount, List<SsqNumbers> numbersList, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        return selectHighestRatio(numbersList, startIn, startIn + nextCount);
//        System.out.println("挑选的红色号码：" + Arrays.toString(selected.getRedNumbers()) + ", 蓝色号码：" + selected.getBlueNumber());
    }


    private static SsqNumbers selectLowestNextCount(int nextCount, List<SsqNumbers> numbersList, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        return selectLowestRatio(numbersList, startIn, startIn + nextCount);
    }

    private static SsqNumbers selectAvgNextCount(int nextCount, List<SsqNumbers> numbersList, int startIn) {

//        System.out.println("剩下： " + nextCount + " 个");
        return selectAvgRatio(numbersList, startIn, startIn + nextCount);
    }


    private static SsqNumbers selectHighestRatio(List<SsqNumbers> numbersList, int startIn, int endEx) {
        int count = endEx - startIn;
        BigDecimal redStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal redStandardRatioAll = redStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int redTotalCount = 6 * count;

        BigDecimal blueStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal blueStandardRatioAll = blueStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int blueTotalCount = count;

        Map<Integer, Integer> redNumberCountMap = new HashMap<>(64);

        for (int i = 1; i < 34; i++) {
            redNumberCountMap.put(i, 0);
        }


        Map<Integer, Integer> blueNumberCountMap = new HashMap<>(32);

        for (int i = 1; i < 17; i++) {
            blueNumberCountMap.put(i, 0);
        }

        for (int i = startIn; i < endEx; i++) {
            SsqNumbers ssqNumbers = numbersList.get(i);
            int[] redNumbers = ssqNumbers.getRedNumbers();

            for (int redNumber : redNumbers) {
                Integer integer = redNumberCountMap.get(redNumber);
                redNumberCountMap.put(redNumber, ++integer);

            }

            int blueNumber = ssqNumbers.getBlueNumber();
            Integer integer = blueNumberCountMap.get(blueNumber);
            blueNumberCountMap.put(blueNumber, ++integer);
        }


        List<Map.Entry<Integer, Integer>> collect = redNumberCountMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
        System.out.println("红色号码数字数量： " + collect);
        List<Map.Entry<Integer, Integer>> blueCol = blueNumberCountMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
        System.out.println("蓝色号码数字数量： " + blueCol);
        List<Map.Entry<Integer, Integer>> entries = collect.subList(0, 6);

        entries.sort(Map.Entry.comparingByKey());
        int[] redNumbers = new int[6];
        for (int i = 0; i < 6; i++) {
            redNumbers[i] = entries.get(i).getKey();
        }
        int blueNumber = blueCol.get(0).getKey();

        return new SsqNumbers(redNumbers, blueNumber);

    }

    private static SsqNumbers selectAvgRatio(List<SsqNumbers> numbersList, int startIn, int endEx) {
        int count = endEx - startIn;
        BigDecimal redStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal redStandardRatioAll = redStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int redTotalCount = 6 * count;

        BigDecimal blueStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal blueStandardRatioAll = blueStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int blueTotalCount = count;

        Map<Integer, Integer> redNumberCountMap = new HashMap<>(64);

        for (int i = 1; i < 34; i++) {
            redNumberCountMap.put(i, 0);
        }


        Map<Integer, Integer> blueNumberCountMap = new HashMap<>(32);

        for (int i = 1; i < 17; i++) {
            blueNumberCountMap.put(i, 0);
        }

        for (int i = startIn; i < endEx; i++) {
            SsqNumbers ssqNumbers = numbersList.get(i);
            int[] redNumbers = ssqNumbers.getRedNumbers();

            for (int redNumber : redNumbers) {
                Integer integer = redNumberCountMap.get(redNumber);
                redNumberCountMap.put(redNumber, ++integer);

            }

            int blueNumber = ssqNumbers.getBlueNumber();
            Integer integer = blueNumberCountMap.get(blueNumber);
            blueNumberCountMap.put(blueNumber, ++integer);
        }

        int redStart = 0;
        int redEnd = 11;
        Random random = new Random();
        int firstBlock1 = random.nextInt(redEnd);
        int firstBlock2;
        do {
            firstBlock2 = random.nextInt(redEnd);
        } while (firstBlock2 == firstBlock1);


        int secondBlock1 = random.nextInt(redEnd) + 11;
        int secondBlock2;
        do {
            secondBlock2 = random.nextInt(redEnd) + 11;
        } while (secondBlock2 == secondBlock1);


        int thirdBlock1 = random.nextInt(redEnd) + 22;
        int thirdBlock2;
        do {
            thirdBlock2 = random.nextInt(redEnd) + 22;
        } while (thirdBlock2 == thirdBlock1);


        List<Map.Entry<Integer, Integer>> collect = redNumberCountMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
        System.out.println("红色号码数字数量： " + collect);
        List<Map.Entry<Integer, Integer>> blueCol = blueNumberCountMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
        System.out.println("蓝色号码数字数量： " + blueCol);
        List<Map.Entry<Integer, Integer>> entries = collect.subList(0, 6);

        entries.sort(Map.Entry.comparingByKey());
        int[] redNumbers = {collect.get(firstBlock1).getKey(), collect.get(firstBlock2).getKey(), collect.get(secondBlock1).getKey(),
                collect.get(secondBlock2).getKey(), collect.get(thirdBlock2).getKey(), collect.get(thirdBlock1).getKey()
        };

        int blueIndex = random.nextInt(6) + 5;

        int blueNumber = blueCol.get(blueIndex).getKey();

        return new SsqNumbers(redNumbers, blueNumber);

    }


    private static SsqNumbers selectLowestRatio(List<SsqNumbers> numbersList, int startIn, int endEx) {
        int count = endEx - startIn;
        BigDecimal redStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal redStandardRatioAll = redStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int redTotalCount = 6 * count;

        BigDecimal blueStandardRatioSingle = BigDecimal.valueOf((6.0F * 100) / 33);
        BigDecimal blueStandardRatioAll = blueStandardRatioSingle.multiply(BigDecimal.valueOf(count));

        int blueTotalCount = count;

        Map<Integer, Integer> redNumberCountMap = new HashMap<>(64);

        for (int i = 1; i < 34; i++) {
            redNumberCountMap.put(i, 0);
        }


        Map<Integer, Integer> blueNumberCountMap = new HashMap<>(32);

        for (int i = 1; i < 17; i++) {
            blueNumberCountMap.put(i, 0);
        }

        for (int i = startIn; i < endEx; i++) {
            SsqNumbers ssqNumbers = numbersList.get(i);
            int[] redNumbers = ssqNumbers.getRedNumbers();

            for (int redNumber : redNumbers) {
                Integer integer = redNumberCountMap.get(redNumber);
                redNumberCountMap.put(redNumber, ++integer);

            }

            int blueNumber = ssqNumbers.getBlueNumber();
            Integer integer = blueNumberCountMap.get(blueNumber);
            blueNumberCountMap.put(blueNumber, ++integer);
        }


        List<Map.Entry<Integer, Integer>> collect = redNumberCountMap.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).collect(Collectors.toList());
        System.out.println("数字数量： " + collect);
        List<Map.Entry<Integer, Integer>> blueCol = blueNumberCountMap.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).collect(Collectors.toList());

        List<Map.Entry<Integer, Integer>> entries = collect.subList(0, 6);

        entries.sort(Map.Entry.comparingByKey());
        int[] redNumbers = new int[6];
        for (int i = 0; i < 6; i++) {
            redNumbers[i] = entries.get(i).getKey();
        }
        int blueNumber = blueCol.get(0).getKey();

        return new SsqNumbers(redNumbers, blueNumber);

    }


    private static List<WinningCondition> winningMap = new ArrayList<>(16);


    static {
        winningMap.add(new WinningCondition(6, 1, 1, -1, "一等奖"));
        winningMap.add(new WinningCondition(6, 0, 2, -1, "二等奖"));
        winningMap.add(new WinningCondition(5, 1, 3, 3000, "三等奖"));
        winningMap.add(new WinningCondition(4, 1, 4, 200, "四等奖"));
        winningMap.add(new WinningCondition(5, 0, 4, 200, "四等奖"));
        winningMap.add(new WinningCondition(3, 1, 5, 10, "五等奖"));
        winningMap.add(new WinningCondition(4, 0, 5, 10, "五等奖"));
        winningMap.add(new WinningCondition(2, 1, 6, 5, "六等奖"));
        winningMap.add(new WinningCondition(1, 1, 6, 5, "六等奖"));
        winningMap.add(new WinningCondition(0, 1, 6, 5, "六等奖"));

    }
    /*
    本期开奖号码:
2 14 19 21 25 28 11
购买号码:
7 14 18 20 23 24 4, 2 4 15 16 28 29 8, 7 8 12 13 14 22 3, 2 10 14 17 18 29 3, 5 13 21 22 26 30 16
     */

    private static void match(String bigWinningNumbers, String boughtNumbers) {
        System.out.println("本期开奖号码是：" + bigWinningNumbers);
        SsqNumbers winningNumbers = parseSsqNumbers(bigWinningNumbers);

        String[] split = boughtNumbers.trim().split(",");
        List<SsqNumbers> bNumbers = new ArrayList<>(split.length);
        for (String s : split) {
            bNumbers.add(parseSsqNumbers(s.trim()));
        }

        doMatch(winningNumbers, bNumbers);
    }

    private static void match(int[] bigWinningNumbers, int[][] boughtNumbers) {
        System.out.println("本期开奖号码是：" + Arrays.toString(bigWinningNumbers));
        SsqNumbers winningNumbers = parseSsqNumbers(bigWinningNumbers);

        List<SsqNumbers> bNumbers = new ArrayList<>(boughtNumbers.length);
        for (int[] s : boughtNumbers) {
            bNumbers.add(parseSsqNumbers(s));
        }

        doMatch(winningNumbers, bNumbers);
    }

    private static void matchNewest(int[][] boughtNumbers) {
        System.out.println("本期开奖号码是：");
        SsqNumbers winningNumbers = parseNewestSsqNumbers();
        if (winningNumbers == null) {
            System.out.println("最新开奖号码未查到");
            return;
        }
        System.out.println(Arrays.toString(winningNumbers.getRedNumbers()) + "/" + winningNumbers.getBlueNumber());


        List<SsqNumbers> bNumbers = new ArrayList<>(boughtNumbers.length);
        for (int[] s : boughtNumbers) {
            bNumbers.add(parseSsqNumbers(s));
        }

        doMatch(winningNumbers, bNumbers);
    }

    private static int doMatch(SsqNumbers winningNumbers, List<SsqNumbers> bNumbers) {

        List<WinningMatchResult> rList = new ArrayList<>(bNumbers.size());

        for (SsqNumbers bNumber : bNumbers) {
            NumbersMatchResult matchResult = bNumber.match(winningNumbers);
            WinningMatchResult winningMatchResult = matchResult.matchWinning(winningMap);
            rList.add(winningMatchResult);
        }
        rList.sort(Comparator.comparingInt(WinningMatchResult::getPrizeClass));
        System.out.println("本期购买号码是：");

        AtomicInteger highClazz = new AtomicInteger(0);

        rList.forEach(r -> {
            NumbersMatchResult numbersMatchResult = r.getNumbersMatchResult();
            SsqNumbers ssqNumbers = numbersMatchResult.getSsqNumbers();
            SsqNumbers winNumbers = numbersMatchResult.getWinNumbers();
            System.out.printf("购买号码：[红(%s), 蓝(%s)]， 奖级：[%s]，金额：[%s]，中奖号码：[红(%s), 蓝(%s)]", Arrays.toString(ssqNumbers.getRedNumbers()), ssqNumbers.getBlueNumber(), r.getPrizeClassDesc(), r.getBonus(), Arrays.toString(winNumbers.getRedNumbers()), winNumbers.getBlueNumber()).println();
            int prizeClass = r.getPrizeClass();
            if (prizeClass > 0 && (prizeClass < highClazz.get() || highClazz.get() == 0)) {

                highClazz.set(prizeClass);
            }
        });

        return highClazz.get();
    }

    private static SsqNumbers parseSsqNumbers(String input) {
        // 6红+1蓝
        String[] wNumbers = input.trim().split(" ");
        int[] redNumbers = new int[6];
        int blueNumber = Integer.parseInt(wNumbers[wNumbers.length - 1].trim());
        for (int i = 0; i < wNumbers.length - 1; i++) {
            redNumbers[i] = Integer.parseInt(wNumbers[i].trim());
        }
        Arrays.sort(redNumbers);
        return new SsqNumbers(redNumbers, blueNumber);
    }

    private static SsqNumbers parseSsqNumbers(int[] wNumbers) {
        // 6红+1蓝

        int[] redNumbers = new int[6];
        System.arraycopy(wNumbers, 0, redNumbers, 0, wNumbers.length - 1);
        Arrays.sort(redNumbers);
        return new SsqNumbers(redNumbers, wNumbers[6]);
    }


    @Data
    @AllArgsConstructor
    private static class WinningCondition {

        private int redCount;

        private int blueCount;

        private int prizeClass;


        private int bonus;

        private String prizeClassDesc;


    }


    @Data
    @AllArgsConstructor
    private static class WinningMatchResult {

        private boolean wined;

        private NumbersMatchResult numbersMatchResult;

        private int prizeClass;

        private String prizeClassDesc;

        private int bonus;

    }

    @Data
    @AllArgsConstructor
    private static class NumbersMatchResult {

        private int redCount;

        private int blueCount;


        private SsqNumbers ssqNumbers;

        private SsqNumbers winNumbers;


        public WinningMatchResult matchWinning(List<WinningCondition> conditions) {
            for (WinningCondition condition : conditions) {
                boolean matched = matchOne(condition);
                if (matched) {
                    return new WinningMatchResult(true, this, condition.getPrizeClass(), condition.getPrizeClassDesc(), condition.getBonus());
                }
            }
            return new WinningMatchResult(false, this, 100, "未中奖", 0);
        }

        public boolean matchOne(WinningCondition condition) {
            return condition.getRedCount() == this.redCount && condition.getBlueCount() == this.blueCount;
        }

    }

    @Data
    private static class SsqNumbers {

        private int[] redNumbers;

        private int blueNumber;

        private String prizeDate;

        public SsqNumbers(int[] redNumbers, int blueNumber) {
            this.redNumbers = redNumbers;
            this.blueNumber = blueNumber;
        }

        public SsqNumbers(int[] redNumbers, int blueNumber, String prizeDate) {
            this.redNumbers = redNumbers;
            this.blueNumber = blueNumber;
            this.prizeDate = prizeDate;
        }

        public NumbersMatchResult match(SsqNumbers ssqNumbers) {
            int[] mRedNumbers = ssqNumbers.getRedNumbers();
            int[] winRedNumbers = new int[mRedNumbers.length];
            int winBlueNumber = 0;
            int redCount = 0;
            int blueCount;
            if (blueNumber == ssqNumbers.blueNumber) {
                blueCount = 1;
                winBlueNumber = blueNumber;
            } else {
                blueCount = 0;
            }

            for (int i = 0, j = 0; i < redNumbers.length && j < mRedNumbers.length; ) {


                int rNumber = redNumbers[i];
                int mRedNumber = mRedNumbers[j];
                if (rNumber == mRedNumber) {
                    winRedNumbers[redCount++] = rNumber;
                    i++;
                    j++;
                    continue;
                }

                if (rNumber < mRedNumber) {
                    i++;
                    continue;
                }

                j++;
            }

            int[] init = new int[6];
            winRedNumbers = ArrayUtils.removeElements(winRedNumbers, init);

            return new NumbersMatchResult(redCount, blueCount, this, new SsqNumbers(winRedNumbers, winBlueNumber));
        }

    }

}
